package cn.samler.shopapi.Service.impl;

import cn.samler.shopapi.Entity.Classify;
import cn.samler.shopapi.Entity.Spec;
import cn.samler.shopapi.Entity.SpecItem;
import cn.samler.shopapi.Mapper.GoodsMapper;
import cn.samler.shopapi.Model.Response.Goods.GoodsBrief;
import cn.samler.shopapi.Model.Response.Goods.GoodsDetails;
import cn.samler.shopapi.Service.GoodsService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    private GoodsMapper goodsMapper;

    GoodsServiceImpl(GoodsMapper goodsMapper) {
        this.goodsMapper = goodsMapper;
    }


    @Override
    public List<Classify> getClassify() {
        return goodsMapper.getClassify();
    }

    @Override
    public List<GoodsBrief> getGoodsByClassify(Integer cid, Integer page, Integer offset) {
        return goodsMapper.getGoodsByClassify(cid, (page - 1) * offset, offset);
    }

    @Override
    public List<GoodsBrief> getGoodsBriefByIdList(List<Integer> idList) {
        return goodsMapper.getGoodsBriefByIdList(idList);
    }

    @Override
    public GoodsDetails getGoodsById(Integer id) {
        GoodsDetails goods = goodsMapper.getGoodsById(id);
        if (goods == null) return null;

        goodsMapper.incrGoodsVisit(id);
        goods.setSpec(goodsMapper.getSpecByGoodsId(goods.getId()));
        goods.getSpec().forEach(spec -> {
            spec.setItem(goodsMapper.getSpecItemBySpecId(spec.getId()));
        });

        return goods;
    }

    @Override
    public List<GoodsBrief> getGoodsByHot() {
        return goodsMapper.getGoodsByHot();
    }

    @Override
    public List<GoodsBrief> getGoodsByNewest() {
        return goodsMapper.getGoodsByNewest();
    }

    @Override
    public Spec getSpecById(Integer gid, Integer id) {
        return goodsMapper.getSpecById(gid, id);
    }

    @Override
    public SpecItem getSpecItemById(Integer sid, Integer id) {
        return goodsMapper.getSpecItemById(sid, id);
    }
}
